package com.geek.gkfood.biz.jms.config;

import com.geek.gkfood.biz.jms.SnatchOrderBatchMqListener;
import com.geek.gkfood.util.enums.mq.MqEnum;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <h1>Jms 消息配置类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/27 21:47
 */
@Configuration
public class JmsConfig {

    @Autowired
    private SnatchOrderBatchMqListener snatchOrderBatchMqListener;

    @Value("${spring.cloud.stream.rocketmq.binder.name-server}")
    private String namesrvAddr;

    @Bean
    public DefaultMQPushConsumer getSnatchOrderBatchMqConsumer() throws Exception{
        // 1、创建消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("SnatchOrderBatchMq_Group");
        // 2、为消费者对象设置 NameServer 地址，批次消费的最大数量
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.setConsumeMessageBatchMaxSize(20);
        // 3、订阅主题
        consumer.subscribe(MqEnum.Q_GKFOOD_SNATCHORDER_BATCH_TOPIC.getCode(), "*");
        // 4、注册监听消息，并打印消息
        consumer.registerMessageListener(snatchOrderBatchMqListener);
        // 5、把消费者直接启动起来
        consumer.start();
        // 6、将消费者放到Spring容器中
        return consumer;
    }
}